package com.example.builder;

public class User {
    private int id;
    private String name;
    private int age;
    private final double PI;

    private User(double PI, int age, String name, int id) {
        this.PI = PI;
        this.age = age;
        this.name = name;
        this.id = id;
    }

    public static Builder builder() {
        return new Builder();
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getPI() {
        return PI;
    }

    /**
     * 静态内部类可以直接访问外部类的私有成员，也可以不需要外部类创建之后再创建，而是可以直接使用
     */
    public static class Builder {
        private int id;
        private String name;
        private int age;
        private static final double PI = 3.14;

        public Builder id(int id) {
            this.id = id;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder age(int age) {
            this.age = age;
            return this;
        }

        public User build() {
            return new User(PI, age, name, id);
        }
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", PI=" + PI +
                '}';
    }
}
